/**
 * check Lib validate
 */
import _ from 'lodash'

class validate {

    constructor() {}
    
    validate(key,value) {

         //console.log('validate',key,value);

         let obj = {bool:true,text:'',value:value};
         let patrn=/^[a-zA-Z_]{1}[a-zA-Z0-9_]{5,17}$/;

         switch(key) {

              case 'username':
                  if (value == null || _.trim(value) == '') {
                        obj.bool = false;
                        obj.text = 'username is required!'
                  } else if (!patrn.exec(value)) {
                        obj.bool = false;
                        obj.text = 'username format is error!';
                  }
              break;

              case 'password':
                  if (value == null || _.trim(value) == '') {
                        obj.bool = false;
                        obj.text = 'password is required!'
                  } else if (value.length < 6 || value.length > 18) {
                        obj.bool = false;
                        obj.text = 'password is length [6,18]!';
                  }
              break;

         }  
         return obj;    
    }

}

var myValidate = new validate();

export default function validate(data) {
    let obj = {};
     
    for (var i in data) {
        obj[i] = myValidate.validate(i,data[i]);
    }
     
    return obj;
}
